package dfl.bysj.service.impl;

import cn.hutool.core.lang.Dict;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dfl.bysj.config.ExcelDictDTOListener;
import dfl.bysj.entity.Academy;
import dfl.bysj.entity.Grade;
import dfl.bysj.entity.Student;
import dfl.bysj.entity.VO.ExcelDictDTO_Student;
import dfl.bysj.mapper.StudentMapper;
import dfl.bysj.service.AcademyService;
import dfl.bysj.service.GradeService;
import dfl.bysj.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DFL
 * @since 2022-05-17
 */
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    private AcademyService academyService;

    private GradeService gradeService;

    @Autowired
    public void setGradeService(GradeService gradeService) {
        this.gradeService = gradeService;
    }

    @Autowired
    public void setAcademyService(AcademyService academyService) {
        this.academyService = academyService;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void importData(InputStream inputStream) {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, ExcelDictDTO_Student.class, new ExcelDictDTOListener(this)).sheet().doRead();
        log.info("importData finished");
    }

    @Override
    public List<ExcelDictDTO_Student> listDictData() {

        List<Student> studentList = baseMapper.selectList(null);

        //创建ExcelDictDTO列表，将Dict列表转换成ExcelDictDTO列表
        ArrayList<ExcelDictDTO_Student> excelDictDTOList = new ArrayList<>(studentList.size());
        studentList.forEach(student -> {

            Grade grade=gradeService.getById(student.getGraId());
            Academy academy=academyService.getById(grade.getAcaId());
            ExcelDictDTO_Student excelDictDTO = new ExcelDictDTO_Student(student,grade,academy);
            excelDictDTOList.add(excelDictDTO);
        });
        return excelDictDTOList;
    }

    @Override
    public void insertBatch(List<ExcelDictDTO_Student> list) {
        log.info("insertBatch");
        List<Student> studentList=new ArrayList<>();
        log.info("insertBatch=>list:{}",list.toString());
        list.stream()
                .forEach(s->{
                    //先找到对应的班级id
                    Academy academy=academyService.getOne(new QueryWrapper<Academy>()
                            .eq("aca_name", s.getAcaName()));
                    if (academy==null){
                        academy=new Academy();
                        academy.setAcaName(s.getAcaName());
                        academyService.save(academy);
                        academy=academyService.getOne(new QueryWrapper<Academy>().
                                eq("aca_name", s.getAcaName()));
                    }

                    Grade grade=gradeService.getOne(new QueryWrapper<Grade>()
                            .eq("gra_name", s.getGraName()));
                    if (grade==null){
                        grade=new Grade();
                        grade.setAcaId(academy.getAcaId());
                        grade.setGraName(s.getGraName());
                        gradeService.save(grade);
                        grade=gradeService.getOne(new QueryWrapper<Grade>()
                                .eq("gra_name", s.getGraName()));
                    }

                    studentList.add(new Student(s,grade.getGraId()));
                });
        saveBatch(studentList);
    }
}
